package com.jiang.mall.mallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jiang.mall.mallorder.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118628292";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpZYoAzvPUrRvijzTp+sPhUGmHtWfxbs56g+OlZcDNW2wO+yVGbjtNkPciHc5YBkoCJM7MGf9xoPNWQNDKWEcbuLiwBn7VOO7PMzOa4vYLgbjVqWmjnSbHoSsJHkdEcIY4joFRgiBWEvi8ORr9VlUfnsMBCGqOq307rteJkQ+1Q5s9cPTE77Jm0nFZOiBP72+DF53plOs/yAijean/H9yk6yOYcD23/7+I+QYV9ifDSWIHHjl1ZyC0nuxpZxaGYG6ZvPXg0/BBaORcxYR1TDwdG5BsLL7oa7bLnHON+2Wrtp78elcHulL9ak2DUonSoqSlkzSqPQxrDTZNTxe4nwibAgMBAAECggEAXcGudS4WNKMvaOEcR1ic6+smzY7xHebXb2XqLJQ59y9fipcklWvTdOYEcqSOnZe1hKQ8tSi6gniz1gx/w07C5KwDxKGDt54QRywups5xogLFe7SNnJoRabJhcGWKym2BTHJLTwK3xQwxEXSF5KvQ53Rmt/6hg5+U6+CkuSZJzGfaakHnVOY9OORuB99VSJHenswCm5TLsTqTHZ2RtYe2CTPP9rd1SCWBg6C1Hh+SY+/eG0UJair/2MEAPuKYo5pH40XEq5QXMTzOxS8qK7R+cqumDdt+f2dmDzU3MPsGeNcZnac1gQQgWiYkkWzSn/cAboF9krt3Y8pxTfu5zP4oKQKBgQD0DIeFGmcd9TZPVQpbMpApCQjMZHbc3PAiZyoELvQSjQ9kbVMZ46UVN+sg/r/3kQQCGq+ZGOyKAsAyBlY7OnesChbfMlGQzq+OxGU+w0h0LQwk0oWtlgbkCIj074V1ZRnCTEyBK8lDCLvdOuG7CreSrRTvk8fwU7v47Iw3CMcYtwKBgQCxsSVi4uCWUCjMWzJzZURK9CK5nJv5zlXsoOCAohFiCzK5LDumYjTIVORc3cINTp6YsaCVD4NN/w2MUqh2zyN9ZJCKzDsARGQQgi4umMd5Ci4At2rrnSYJIu/9+piUwtC0M1arZFxFhywdeOUzgFXyky6cO7ikmOG8d9NChzwDPQKBgQDphJej9+iVVM4byN549v/UFA30LWlC2rF36cjdaeQnYAxLZwRuMFIE5JW646fiBkOUk95UGvIy+IDZHREj6UUbPCg4d1huvCZl8GyESq2oZHZLQZmhyH1dcwFBhXLpKs0kCoflzXTuDIxeGnpl09JlqfHvcDOPNUAW5pkgC+keLwKBgCDlf9tnz1ZkrrjupU9xzwvlWMxuLv43cfbl7cpCtt8k3ZYkl0J9afIwrTp4H6XhCUGRmP4fqhp1fqec4e1k+dgMFyprFv08rB3WTAB6bmt9M/kT++p05IbKfg3SKA9aZdMGsiP255JDrSojqKM5PjAziWyJLezjFN+Ue3sUmEqtAoGALBBaRfnKIyhFuqqUWSRR9PB4yefpvZXTfuSGC932xJ9eVxENeoobeGCQ3BWf8jsfrhO3OCCUnr5HODx9sE1kSuq0uAPQEQI8ySNzWRbuyNds3XNpDwLGEkSwAeNhxriaMYVqCkC06WzEb+Sz27lftK1ifq4r7AChcV8FLZ6DD5E=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqgDP8aKyE38q9im1w4gZ4dOtV6cho68IvZVdbb78SkgANxTvE2CubvkadsQx4Uj1mwLB1HGc3M1vUjrgYpMEKAC+bPxzOb59OXjUMUzY9/5sPxpI7NbPy8ZxApasd9jv3lqvzrQYcApM/DwIziB/U2L1/CY8TZ1uy1J7gebEmgk2YBdmMascIh7WrViS6I3CzMREPCvuh4ImBvD7aJeNJVkje75vGh92ioCXze83MeVpXyH3sUAtiMgPoCUA9ukduPO/gRkc5Zi8HbFF+K5PaeGIaVSgHX5McAFEjWtBFON06Fhms+MJcqCGVrsRoiJ4fvJw59xrCwzTu90AynVHqwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://d5bhjo16b3.51xd.pub/payed/notify";



    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\"," // 使用超时时间，订单超时自动失效
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
